/**
 * DESC: Tests polygon drawing without fill.
 *
 * IMP: HIGH
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>

#include <object.h>
#include <polygon.h>
#include <surface.h>
#include <visual.h>

#include <debug.h>
#include <unittest.h>


int main( int argc, char *argv[] )
{
  debug_level = 0; /* 1 = warn, 2 = info, 3 = verbose, 4 = debug mode */

  printf( "Polygon drawing test.\n" );
  printf( "Press any key to go next test step.\n" );


  visual_init( 800, 600 );

  coord_t p = { 0, 0 };
  surface_t *s = surface_new( 800, 600 );
  assert_not_null( s );

  polygon_t *l = polygon_new();
  assert_not_null( l );

  color_t c;
  COLOR_FROM_ARGB( c, COLOR_RED );
  object_init3( l, c, 0.5 );

  char *t;

  COLOR_FROM_ARGB( OBJECT_COLOR( l ), COLOR_GREEN );

  polygon_add_vertex( l, 100, 100 );
  polygon_add_vertex( l, 100, 160 );
  polygon_add_vertex( l, 160, 200 );
  polygon_add_vertex( l, 280, 100 );
  polygon_add_vertex( l, 280, 160 );
  polygon_add_vertex( l, 220, 100 );

  object_draw( l, s );
  t = object_str( l );
  printf( "%s\n", t );
  free( t );

  object_draw( l, s );

  visual_display( s, p );
  visual_update();
  visual_waitkey();

  surface_clear( s );

  COLOR_FROM_ARGB( OBJECT_COLOR( l ), COLOR_GREEN );
  OBJECT_SET_DRAW_OPT( l, DRAW_FILL );
  polygon_clear_vertexes( l );

  polygon_add_vertex( l, 100, 100 );
  polygon_add_vertex( l, 100, 160 );
  polygon_add_vertex( l, 160, 200 );
  polygon_add_vertex( l, 280, 100 );
  polygon_add_vertex( l, 280, 160 );
  polygon_add_vertex( l, 220, 100 );

  object_draw( l, s );
  t = object_str( l );
  printf( "%s\n", t );
  free( t );

  visual_display( s, p );
  visual_update();
  visual_waitkey();

  surface_clear( s );

  COLOR_FROM_ARGB( OBJECT_COLOR( l ), COLOR_RED );
  polygon_clear_vertexes( l );
  polygon_add_vertex( l, 200, 200 );
  polygon_add_vertex( l, 500, 300 );
  polygon_add_vertex( l, 300, 100 );
  polygon_add_vertex( l, 700, 200 );

  t = object_str( l );
  printf( "%s\n", t );
  free( t );

  object_draw(  l, s );

  visual_display( s, p );
  visual_update();
  visual_waitkey();

  COLOR_FROM_ARGB( OBJECT_COLOR( l ), COLOR_MAGENTA );
  polygon_clear_vertexes( l );
  polygon_add_vertex( l, 100, 300 );
  polygon_add_vertex( l, 200, 300 );
  polygon_add_vertex( l, 200, 400 );
  polygon_add_vertex( l, 100, 400 );

  t = object_str( l );
  printf( "%s\n", t );
  free( t );

  object_draw( l, s );

  visual_display( s, p );
  visual_update();
  visual_waitkey();

  surface_clear( s );

  COLOR_FROM_ARGB( OBJECT_COLOR( l ), COLOR_GREEN );
  polygon_clear_vertexes( l );
  polygon_add_vertex( l, 200, 150 );
  polygon_add_vertex( l, 300, 300 );
  polygon_add_vertex( l, 100, 200 );
  polygon_add_vertex( l, 300, 200 );
  polygon_add_vertex( l, 100, 300 );

  t = object_str( l );
  printf( "%s\n", t );
  free( t );

  object_draw( l, s );

  COLOR_FROM_ARGB( OBJECT_COLOR( l ), COLOR_RED );
  OBJECT_UNSET_DRAW_OPT( l, DRAW_FILL );
  polygon_clear_vertexes( l );
  polygon_add_vertex( l, 200, 150 + 200 );
  polygon_add_vertex( l, 300, 300 + 200 );
  polygon_add_vertex( l, 100, 200 + 200 );
  polygon_add_vertex( l, 300, 200 + 200 );
  polygon_add_vertex( l, 100, 300 + 200 );

  t = object_str( l );
  printf( "%s\n", t );
  free( t );

  object_draw( l, s );


  visual_display( s, p );
  visual_update();
  visual_waitkey();

  COLOR_FROM_ARGB( OBJECT_COLOR( l ), COLOR_BLUE );
  OBJECT_SET_DRAW_OPT( l, DRAW_FILL );
  polygon_clear_vertexes( l );
  polygon_add_vertex( l, 200 + 300 , 150 );
  polygon_add_vertex( l, 100 + 300 , 200 );
  polygon_add_vertex( l, 100 + 300 , 300 );
  polygon_add_vertex( l, 300 + 300 , 300 );
  polygon_add_vertex( l, 300 + 300 , 200 );


  t = object_str( l );
  printf( "%s\n", t );
  free( t );

  object_draw( l, s );

  visual_display( s, p );
  visual_update();
  visual_waitkey();


  surface_free( s );
  object_free( l );

  visual_quit();

  return ask_user_yn( "Everything rendered as expect?" ) != 1;
}
